
import instance from "../plugins/ajax";

//根据type修改chooseCount
const dealWithSelectItem = (state, action, type) =>{
  let listData = Object.assign({}, state.book_list_data);
  let currentItem=[];
  // 找到外层，左边list列表
  let list = listData.food_spu_tags || [];
  if(action.outIndex||action.outIndex==0){
    currentItem = list[action.outIndex]
  }else{
    currentItem = list[state.current_left_index]
  }

  // 通过列表找到左边item使用的数据也就是点击的item数据
  // 对当前点击这个item的chooseCount加一或减一
  if (type === 1) {
    currentItem.spus[action.index].chooseCount ++;
  } else {
    currentItem.spus[action.index].chooseCount --;
  }


  return listData;
}

// 将chooseCount重置为0
const dealclear=(state)=>{
  let listData = Object.assign({}, state.book_list_data);
  let list = listData.food_spu_tags||[];

  for(let i=0;i<list.length;i++){
    for(let k=0;k<list[i].spus.length;k++){
      list[i].spus[k].chooseCount=0;
    }
  }
  return listData
}

//分类tabs
let tabs = {};

tabs['cate'] = {
  key: 'cate',
  text: '全部分类',
  id:""
};
tabs['type'] = {
  key: 'type',
  text: '综合排序',
  id:""
};
tabs['filter'] = {
  key: 'filter',
  text: '筛选',
  id:""
};
//详情tabs
let detail_tabs= [
  {
    name: '点菜',
    key: 'menu'
  },
  {
    name: '评价',
    key: 'comment'
  },
  {
    name: '商家',
    key: 'restanurant'
  }
];

export const state = () => ({
  home_list: [],
  order_list:[],
  cate_list:[],
  ready_to_load:true,
  is_end:false,
  tabs:tabs,
  active_key:"",
  close_panel_current:true,
  filter_data:{},
  type:"",
  cate_id:'',
  cate_page:1,
  detail_tabs:detail_tabs,
  book_list_data:{},
  show_choose_content:false,
  current_left_index:0,
  poi_name:'',
  detail_list:[]
})

export const mutations = {
  setDetailList(state,detail_list){
    state.detail_list=detail_list
  },
  setCurrentLeftIndex(state,i){
    state.current_left_index=i
  },
  setHomeList (state,home_list) {
    state.home_list = home_list
  },
  setOrderList (state,order_list) {
    state.order_list = order_list
  },
  setFilterData(state,filter_data){
    state.filter_data=filter_data
  }
  ,showChooseContentChange(state){
    state.show_choose_content=!state.show_choose_content
  }
  ,
  setReadyToLoad (state,ready_to_load) {
    state.ready_to_load = ready_to_load
  },
  setActiveKey(state,active_key) {
    state.active_key = active_key
  },
  setClosePanelCurrent(state,close_panel_current) {
    state.close_panel_current = close_panel_current
  },
  setIsEnd(state,is_end){
    state.is_end=is_end;
  },
  setCateId(state,cate_id){
    state.cate_id=cate_id;
  },
  setType(state,type){
    state.type=type;
  },
  setCateList(state,cate_list){
    state.cate_list=cate_list
  },
  setCatePage(state,cate_page){
    state.cate_page=cate_page
  },
  setTabs(state,tabs){
    state.tabs=tabs
  },
  setBookListData(state,book_list_data){
    state.book_list_data=book_list_data;
  },
  minus(state,data){
    state.book_list_data=Object.assign({},dealWithSelectItem(state,data,0))
  },
  plus(state,data){
    state.book_list_data=Object.assign({},dealWithSelectItem(state,data,1))
  },
  clearAll(state){
    state.book_list_data=Object.assign({},dealclear(state))
    state.show_choose_content=false
  },
  setPoiName(state,poi_name){
    state.poi_name=poi_name
  }
}



export const actions = {
  // nuxtServerInit is called by Nuxt.js before server-rendering every page
  async nuxtServerInit({commit,dispatch}, { query }) {
    await dispatch('homeList')
    await dispatch('orderList')
    await dispatch('filterData')
    await dispatch('cateQuery',['0','cate',1])
    if(query.id){
      await dispatch('detailBook',query.id)
      await  dispatch('detailEva',[query.id,1])
    }else {
      await dispatch('detailBook',0)
      await  dispatch('detailEva',[0,1])
    }
    commit('setReadyToLoad',true)
    commit('setIsEnd',false)
  },
  //加载首页内容
  async homeList({commit,state},page=1){
    commit('setReadyToLoad',false)
    const { data } = await instance.get("/app2/json/list.json",{params:{page:page}})
    if(page==1){
      commit('setIsEnd',false)
      commit('setHomeList',data.data.poilist)
    }else{
      commit('setHomeList',state.home_list.concat(data.data.poilist))
    }
    commit('setReadyToLoad',true)
    if(page>2){
      commit('setIsEnd',true)
    }

  },
  //加载首页订单内容
  async orderList({commit,state},page=1){
    commit('setReadyToLoad',false)
    const { data } = await instance.get("/app2/json/orders.json",{params:{page:page}})
    if(page==1){
      commit('setIsEnd',false)
      commit('setOrderList',data.data.digestlist)
    }else{
      commit('setOrderList',state.order_list.concat(data.data.digestlist))
    }
    commit('setReadyToLoad',true)
    if(page>2){
      commit('setIsEnd',true)
    }

  },
  //加载分类内容
  async filterData({commit,state}){
    const { data } = await instance.get("/app2/json/filter.json")
    commit('setFilterData',data.data)
  },
  //切换tab
  changeTab({commit,state},data){
    commit('setActiveKey',data[0])
    commit('setClosePanelCurrent',data[1])
  },
  //加载分类内容
  async cateQuery({commit,state},value){
    commit('setCateId',value[0])
    commit('setType',value[1])
    commit('setCatePage',value[2])
    commit('setClosePanelCurrent',true)
    let _tabs=Object.assign({}, state.tabs);
    if(value.length==4){

      _tabs[value[1]]={
        key:_tabs[value[1]].key,
        text: value[3],
        id:value[0]
      }
      commit('setTabs',_tabs)
    }
    if(value[2]>2){
      commit('setIsEnd',true)
    }else{
      commit('setIsEnd',false)
    }
    let fliter={}
    for (let i in _tabs){
      fliter[i]=_tabs[i].id
    }

    if(value[0]!='0'){
      const { data } = await instance.get("/app2/json/listparams.json",{params:{page:value[2],fliter:fliter}})
      if(value[2]==1){
        commit('setIsEnd',false)
        commit('setCateList',data.data.poilist)
      }else{
        commit('setCateList',state.cate_list.concat(data.data.poilist))
      }
      return ;
    }
    const { data } = await instance.get("/app2/json/homelist.json",{params:{page:value[2],type:value[1],id:value[0]}})
    if(value[2]==1){
      commit('setIsEnd',false)
      commit('setCateList',data.data.poilist)
    }else{
      commit('setCateList',state.cate_list.concat(data.data.poilist))
    }
  },
  //加载详情页点餐内容
  async detailBook({commit,state},value=100011){
    const { data } = await instance.get("/app2/json/food.json",{params:{id:value}})
    commit('setBookListData',data.data)
    commit('setPoiName',data.data.poi_info.name)
  },
  // 加载详情评价内容
  async detailEva({commit,state},[value=100011,page=1]){
    commit('setReadyToLoad',false)
    const { data } = await instance.get("/app2/json/comments.json",{params:{id:value,page:page}})
    if(page==1){
      commit('setIsEnd',false)
      commit('setDetailList',data.data.comments)
    }else{
      commit('setDetailList',state.detail_list.concat(data.data.comments))
    }
    commit('setReadyToLoad',true)
    if(page>2){
      commit('setIsEnd',true)
    }
  }
}


